//
// Created by hp on 2020-01-04.
//

#include "Bullet.h"

Bullet::Bullet(){

}
Bullet::Bullet( vector<string> paths, string expSound,point point1,bool is,int t, int w = 20, int h = 40, int v = 500)
: Gameobject(paths, expSound,point1, w, h, v) {
    type=t;
    is_enemy=is;
    harm=type*5;
    have_been_exploded= false;
}

Bullet::~Bullet(){

}

bool Bullet::checkEdge() {
    if(is_enemy) {
        return  pos.y <= 0;
    } else{
        return pos.y >= Game::Height - hei;
    }
}



bool Bullet::checkCollision(vector<Flight*> enemy) {
    point bul_LDOWN=pos;
    point bul_RUP={pos.x+wid,pos.y+hei};
    point fli_LDOWN;
    point fli_RUP;
    bool Xcollision;
    bool Ycollision;
    for (int i = 0; i < enemy.size(); ++i) {
        fli_LDOWN={enemy[i]->pos.x,enemy[i]->pos.y+hei/2};
        fli_RUP={enemy[i]->pos.x+enemy[i]->wid,enemy[i]->pos.y+enemy[i]->hei};
        Xcollision = !(bul_LDOWN.x > fli_RUP.x || bul_RUP.x < fli_LDOWN.x);
        Ycollision = !(bul_LDOWN.y > fli_RUP.y || bul_RUP.y < fli_LDOWN.y);
        if(Xcollision&&Ycollision) {
            if(!have_been_exploded) {
                enemy[i]->beingHurt(harm);
                //have_been_exploded= true;
            }
            return true;
        }
    }
    return false;
}